package com.nebula.tlggoods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nebula.tlggoods.entity.Goods;
import com.nebula.tlggoods.mapper.GoodsMapper;
import com.nebula.tlggoods.service.GoodsService;
import com.nebula.tlggoods.tools.Record;
import com.nebula.tlggoods.tools.Status;
import com.nebula.tlggoods.tools.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商品实现类
 */
@Service()
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 模糊查询并分页、排序
     *
     * @param pageNum  页码
     * @param pageSize 每页显示条数
     * @param keyword  关键字 用于搜索功能模糊查询商品
     * @param shopId   店铺id  用于查询店铺所有商品
     * @param sortFlag 排序 0综合，1销量，2价格，3访问量
     * @return
     */
    @Override
    public Record findAllByFuzzyNameAndPaging(Integer pageNum, Integer pageSize, String keyword, Integer shopId, Integer sortFlag) {
        Record record = new Record();
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == sortFlag) {
            sortFlag = 0;
        }
        List<Goods> list = null;
        String key = "goodsPagingPageNum" + pageNum + "pageSize" + pageSize + "sortFlag" + sortFlag + "keyword" + keyword + "shopId" + shopId;
        ValueOperations<String, List<Goods>> operations = redisTemplate.opsForValue();
        try {
            PageHelper.startPage(pageNum, pageSize);
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = goodsMapper.findAllByFuzzyNameAndPaging(keyword, shopId, sortFlag);
                //添加緩存 有效期五分钟
                operations.set(key, list, 5, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("商品列表查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != list && list.size() > 0) {
            PageInfo<Goods> pageInfo = new PageInfo<>(list);
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("商品列表查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("商品列表查询失败！！");
        }
        return record;
    }


    /**
     * 热门推荐
     *
     * @return
     */
    @Override
    public Record findPopularGoods() {
        Record record = new Record();
        List<Goods> list = null;
        String key = "popularGoodsList";
        ValueOperations<String, List<Goods>> operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = goodsMapper.findPopularGoods();
                //添加緩存  5分钟
                operations.set(key, list, 5, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("热门推荐查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("热门推荐查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("热门推荐查询失败！！");
        }
        return record;
    }


    /**
     * 根据商品编码得到商品详情
     *
     * @param goodsCode 商品编码
     * @return
     */
    @Override
    public Record findGoodsParticularsByGoodsCode(String goodsCode) {
        Record record = new Record();
        Goods goods = null;
        String key = "goodsParticulars" + goodsCode;
        ValueOperations<String, Goods> operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                goods = operations.get(key);
            } else {
                goods = goodsMapper.findGoodsParticularsByGoodsCode(goodsCode);
                // 写入缓存 3分钟
                operations.set(key, goods, 3, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据商品编码得到商品详情查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (goods != null) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("根据商品编码得到商品详情查询成功");
            record.setData(goods);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据商品编码得到商品详情查询失败！！");
        }
        return record;
    }

    /**
     * 根据三级分类id得到所有同分类商品
     *
     * @param typeId 三级分类id
     * @return
     */
    @Override
    public Record findGoodsByTypeId(Integer typeId) {
        Record record = new Record();
        List<Goods> list = null;
        try {
            list = goodsMapper.findGoodsByTypeId(typeId);
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据三级分类id得到商品查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("根据三级分类id得到商品查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据三级分类id得到商品查询失败！！");
        }
        return record;
    }

    /**
     * 相似推荐
     * 根据商品编码得到对应的分类id,查询同分类访问量前十的商品
     *
     * @param goodsCode 商品编码
     * @return
     */
    @Override
    public Record findSimilarityGoods(String goodsCode) {
        Record record = new Record();
        List<Goods> list = null;
        String key = "similarityGoods" + goodsCode;
        ValueOperations<String, List<Goods>> operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = goodsMapper.findSimilarityGoods(goodsCode);
                //添加緩存 3分钟
                operations.set(key, list, 3, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("相似商品推荐查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("相似商品推荐查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("相似商品推荐查询失败！！");
        }
        return record;
    }

    /**
     * 上下架
     *
     * @param saleId  0下架，1上架
     * @param goodsId 商品id
     * @return
     */
    @Transactional
    @Override
    public Record isSale(Integer saleId, Integer goodsId) {
        Record record = new Record();
        int count = 0;
        try {
            count = goodsMapper.isSale(saleId, goodsId);
        } catch (Exception e) {
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("修改异常！！！");
            e.printStackTrace();
            return  record;
        }
        if (count > 0) {
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("修改成功");
        } else {
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("修改失败！！");
        }
        return record;
    }


    /**
     * 删除商品
     *
     * @param goodsId 商品id
     * @return
     */
    @Transactional
    @Override
    public Record delGoods(Integer goodsId) {
        Record record = new Record();
        int count = 0;
        try {
            count = goodsMapper.delGoods(goodsId);
        } catch (Exception e) {
            record.setStatus(Status.REMOVE_FALIED);
            record.setMsg("删除异常！！！");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.REMOVE_SUCCESS);
            record.setMsg("删除成功");
        } else {
            record.setStatus(Status.REMOVE_FALIED);
            record.setMsg("删除失败！！");
        }
        return record;
    }

    /**
     * 统计商品数量
     *
     * @param keyword 关键字
     * @return
     */
    @Override
    public Record countGoodsQuantity(String keyword) {
        Record record = new Record();
        Integer count = null;
        String key = "goodsQuantity" + keyword;
        ValueOperations operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                count = (int) operations.get(key);
            } else {
                count = goodsMapper.countGoodsQuantity(keyword);
                //添加缓存 3分钟
                operations.set(key, count, 3, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("统计商品数量异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != count) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("统计商品数量成功");
            record.setData(count);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("统计商品数量失败！！");
        }
        return record;
    }

    /**
     * 添加商品
     *
     * @param goods 商品对象
     * @return
     */
    @Transactional
    @Override
    public Record saveGoods(Goods goods) {
        Record record = new Record();
        int count = 0;
        try {
            goods.setGoodsCode(UUIDUtil.getNumber(8));
            count = goodsMapper.saveGoods(goods);
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("添加商品异常！！！");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("添加商品成功");
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("添加商品失败！！");
        }
        return record;
    }

    /**
     * 修改商品
     *
     * @param goods 商品对象
     * @return
     */
    @Transactional
    @Override
    public Record modifyGoods(Goods goods) {
        Record record = new Record();
        int count = 0;
        try {
            count = goodsMapper.modifyGoods(goods);
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("修改商品异常！！！");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("修改商品成功");
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("修改商品失败！！");
        }
        return record;
    }
}
